package net.kehk.towerdefense.gamelogic;

import java.util.ArrayList;
import java.util.LinkedList;
import com.badlogic.gdx.math.Ellipse;

public class Tower extends DrawableEntity{
	int damage;
	float attack_speed;
	float attack_cooldown;
	Ellipse range;
	LinkedList<Enemy> target_list;
	public Tower(float pos_x, float pos_y, int damage, float attack_speed, float range){
		super(pos_x, pos_y, 0, 32, 32);
		this.damage=damage;
		this.attack_speed=attack_speed;
		this.target_list=new LinkedList<Enemy>();
		this.range=new Ellipse(this.getHitbox().x, this.getHitbox().y, range, range);
	}

	private void attack(int delta, ArrayList<Projectile> projectile_list){
		if(attack_cooldown<=attack_speed){
			//TODO caster cette fonction proprement
			projectile_list.add(new Projectile(this.getHitbox().x, this.getHitbox().y, 1, 5, 5, target_list.peek()));
			target_list.peek().takeDamage(damage);
		}
		else attack_cooldown+=delta;
	}

	public void updateTargets(ArrayList<DrawableEntity> list){
		for(Enemy e : target_list){
			if(!isInRange(e) || e.getCurrentHealth()==0) target_list.remove(e);
		}

		for(DrawableEntity e : list){
			if(e instanceof Enemy && isInRange(e) && !this.target_list.contains(e)) target_list.add((Enemy)e);
		}
	}

	private boolean isInRange(DrawableEntity e){
		//TODO formule de détection de collision
		return false;
	}

	public void update(ArrayList<DrawableEntity> entity_list, ArrayList<Projectile> projectile_list, int delta){
		this.updateTargets(entity_list);
		this.attack(delta, projectile_list);
	}
}